<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<%@page import="java.util.*"%>
<%@page import="avicit.platform6.api.syslookup.dto.SysLookupSimpleVo"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<%@ page import="avicit.platform6.commons.utils.ViewUtil"%>
<%@ taglib prefix="sec" uri="/WEB-INF/tags/shiro.tld"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@page import="avicit.platform6.core.locale.PlatformLocalesJSTL"%>
<%@page import="java.util.Locale"%>
<%@page import="avicit.platform6.api.session.SessionHelper"%>

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>设置数据源</title>
    <base href="<%=ViewUtil.getRequestPath(request)%>">
    <%
        Locale locale =SessionHelper.getLocaleByUser(request);
    %>
    <%
        String sysId = request.getParameter("sysId");
    %>
    <jsp:include page="/avicit/platform6/component/common/EasyUIJsInclude.jsp"></jsp:include>
    <jsp:include page="/avicit/im/common/ImUIJSCSSInclude.jsp"></jsp:include>

    <style type="text/css">
        .inputbox {
            background-color: #fff;
            border: 1px solid #95b8e7;
            color: #000;
            height: 18px;
        }
    </style>

    <script type="text/javascript">
        var _sysId = '${sysId}';
        var _dialogId = '${dialogId}';
        var _parentId = '${parentId}';
        var templateId = "";
        $(function(){
            $("#dataSourceIframe").attr("src", "platform/im/lantop/basic/ReportManagerController/databasesourceconfig?sysId=" + _sysId + "&dialogId=" + _dialogId + "&parentId=" + _parentId);

            $("#tt").tabs({
                onSelect : function(title,index){
                    if(1 == index){
                        // 关联关系
                        if( ! $("#dataSourceIframe")[0].contentWindow.endEditing()){
                            $("#tt").tabs('select', 0);
                            return ;
                        }
                        // 排序关系
                        if( ! $("#dataSourceIframe")[0].contentWindow.endEditingSort()){
                            $("#tt").tabs('select', 0);
                            return ;
                        }
                        // SQL 选项卡， 根据参数获取SQL语句
                        var args = $("#dataSourceIframe")[0].contentWindow.getSaveArgs();
                        var hasProp = false;
                        for(var prop in args){
                            hasProp = true;
                            break;
                        }
                        if(! hasProp){
                            //'提示', '当前没有选择字段！'
                            $.messager.alert("<%=PlatformLocalesJSTL.getBundleValue("lantop.remind","i18n.lantop",locale)%>", "<%=PlatformLocalesJSTL.getBundleValue("lantop.basicdatamanager.reportmanager.databasesource.nochoosefield","i18n.lantop",locale)%>", 'warning');
                            return ;
                        }
                        var fieldInfos = args.fieldInfos;
                        var relation = args.relation;
                        var tableNames = args.tableNames;
                        var sort = args.sort;
                        var sql = "<span style='color:blue'>SELECT</span>  ";
                        // 生成SQL语句
                        $.each(fieldInfos, function(index, o){
                            sql = sql + o.TABLE_NAME + "." + o.COL_NAME + ",<br/>" ;
                            sql = sql + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";

                        });
                        sql = sql.substring(0,sql.length- 48);
                        sql = sql + "<br/></br/>";
                        sql = sql + "<span style='color:blue'>FROM</span> &nbsp;&nbsp;";
                        $.each(tableNames, function(index , o){
                            sql = sql + tableNames + ",";

                        });
                        sql = sql.substring(0,sql.length-1);
                        sql = sql + "<br/><br/>";
                        $.each(relation, function(index, o){
                            if(0 == index){
                                sql = sql + "<span style='color:blue'>WHERE</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" +  o.COLNAME + "&nbsp" + o.OPRATION + "&nbsp" + o.COLNAME1 + "<br/>";
                            }else{
                                sql = sql + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" + "<span style='color:blue'>AND</span> "  + o.COLNAME  + "&nbsp" + o.OPRATION + "&nbsp" +  o.COLNAME1 + "<br/>";
                            }
                        });
                        sql = sql + "<br/>";
                        $.each(sort, function(index, o){
                            if(0 == index){
                                sql = sql + "<span style='color:blue'>ORDER BY</span> &nbsp;&nbsp;" +  o.COLNAME3 + "&nbsp;" + o.SORT +  ",";
                            }else{
                                sql = sql+ o.COLNAME3 + "&nbsp;" + o.SORT + ",";
                            }

                        });
                        sql = sql.substring(0,sql.length-1);
                        $("#sql").html(sql);
                    }
                }
            });
        });

        function save(){
            // 关联关系
            if( ! $("#dataSourceIframe")[0].contentWindow.endEditing()){
                return ;
            }
            // 排序关系
            if( ! $("#dataSourceIframe")[0].contentWindow.endEditingSort()){
                return ;
            }
            var saveArgs = $("#dataSourceIframe")[0].contentWindow.getSaveArgs();
            var hasProp = false;
            for(var prop in saveArgs){
                hasProp = true;
                break;
            }
            if(! hasProp){
                //'提示', '没有可供保存的字段！',
                $.messager.alert("<%=PlatformLocalesJSTL.getBundleValue("lantop.remind","i18n.lantop",locale)%>", "<%=PlatformLocalesJSTL.getBundleValue("lantop.basicdatamanager.reportmanager.databasesource.nosavefield","i18n.lantop",locale)%>", 'warning');
                return ;
            }
            // 这里发送AJAX请求 , 保存数据
            var reportName = $.trim($("#reportName").val());
            if("" == reportName){
                $("#reportName").val('');
                //'提示', '模板名称不能为空！',
                $.messager.alert("<%=PlatformLocalesJSTL.getBundleValue("lantop.remind","i18n.lantop",locale)%>", "<%=PlatformLocalesJSTL.getBundleValue("lantop.basicdatamanager.reportmanager.databasesource.templatenotnull","i18n.lantop",locale)%>", 'warning');
                return ;
            }
            if(reportName.length >= 125){
                //'提示','模板名称超过长度范围！'
                $.messager.alert("<%=PlatformLocalesJSTL.getBundleValue("lantop.remind","i18n.lantop",locale)%>", "<%=PlatformLocalesJSTL.getBundleValue("lantop.basicdatamanager.reportmanager.databasesource.templatenametoolong","i18n.lantop",locale)%>", 'warning');
                return;
            }
            var otherArgs = {
                text : reportName,			// 报表名称
                reportType : '1',			// 报表类型, 0 为分类, 1为模板
                id : templateId
            };
            $.extend(saveArgs , otherArgs);
            $.ajax({
                url : 'platform/im/lantop/basic/ReportManagerController/savedatabaseconfig',
                type : 'post',
                dataType : 'json',
                data : {
                    param : JSON.stringify(saveArgs)
                },
                success : function(result){
                    if (result.flag == "failure") {
                        $.messager.show({
                            //'提示'
                            title : "<%=PlatformLocalesJSTL.getBundleValue("lantop.remind","i18n.lantop",locale)%>",
                            //"保存失败!"
                            msg : "<%=PlatformLocalesJSTL.getBundleValue("lantop.save.failure","i18n.lantop",locale)%>"
                        });
                    } else {
                        templateId = result.templateId;
                        var dlg = $.messager.show({
                            //'提示'
                            title : "<%=PlatformLocalesJSTL.getBundleValue("lantop.remind","i18n.lantop",locale)%>",
                            //"保存成功！"
                            msg : "<%=PlatformLocalesJSTL.getBundleValue("lantop.save.success","i18n.lantop",locale)%>",
                            timeout : 0
                        });
                        // 使用该方法可以自定义一些关闭事件
                        setTimeout(function(){
                            dlg.window('close');
                            // 刷新父页面的树
                            parent.reportManager.reLoadReportTreeRoot();
                            parent.reportManager.reLoadReportTree();
                            // 保存成功后，关闭页面
                            parent.$('#' + _dialogId).dialog('close');
                        }, 2000);
                    }
                }
            });

        }

        function cancel(){
            // 需要刷新父窗口左侧的树
            parent.reLoadTreeRoot();
            parent.reportManager.reLoadReportTree();
            parent.$('#' + _dialogId).dialog('close');
        }

    </script>


</head>
<body id="myBody" class="easyui-layout" data-options="fit:true">

<div data-options="region:'north',noheader:true,split:false" style="height:40px;" class = "datagrid-toolbar">
    <div style="padding-left:15px; padding-top:0px;">
        <table>
            <tr>
                <%--请输入模板名称: --%>
                <th><%=PlatformLocalesJSTL.getBundleValue("lantop.basicdatamanager.reportmanager.databasesource.inputtemplatename","i18n.lantop",locale)%></th>
                <td width="350px"><input id="reportName" class="easyui-validatebox" data-options="required:true" style="width:350px;"></td>
                <%--保存 --%>
                <td><a class="easyui-linkbutton"  data-options="plain:true, iconCls:'icon-im-save'" href="javascript:void(0)" onclick="save()"><%=PlatformLocalesJSTL.getBundleValue("lantop.save","i18n.lantop",locale)%></a></td>
                <%--关闭--%>
                <td><a class="easyui-linkbutton" plain="true" iconCls="icon-im-cancel"
                       onclick="cancel();" href="javascript:void(0);" ><%=PlatformLocalesJSTL.getBundleValue("lantop.basicdatamanager.reportmanager.databasesource.close","i18n.lantop",locale)%></a></td>
            <tr>
        </table>
    </div>
</div>

<div data-options="region:'center',noheader:true,split:false, fit:true" style = "padding:0px;background:#eee;" >
    <div id="tt" class="easyui-tabs" data-options="border:'false', fit:true" >
        <%--数据源设置--%>
        <div title="<%=PlatformLocalesJSTL.getBundleValue("lantop.basicdatamanager.reportmanager.databasesource.datasouceset","i18n.lantop",locale)%>" data-options="closable:false"
             style="overflow: hidden;" id="tabDataSource">
            <iframe id="dataSourceIframe" name="dataSourceIframe"
                    style="width: 100%; height: 90%" frameborder="0"
                    overflow="hidden">
            </iframe>
        </div>
        <%--SQL语法--%>
        <div title="<%=PlatformLocalesJSTL.getBundleValue("lantop.basicdatamanager.reportmanager.databasesource.sqlgrammar","i18n.lantop",locale)%>" data-options="closable:false"
             style=" background-color:#E3EFFB;padding: 0px;overflow-y:auto;" width = "100%"  height="90%" id="tabSQL">
            <!--  -->
            <div style="overflow-y:auto;height:340px;font-weight:bold" id="sql">

            </div>
        </div>
    </div>

</div>



</body>
</html>
